home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / PropertyChangeSupport.java < prev    next >
Text File  |  1998-09-22  |  4KB  |  139 lines

  1. /*
  2.  * @(#)PropertyChangeSupport.java    1.13 98/07/01
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.beans;
  16.  
  17. import java.io.Serializable;
  18. import java.io.ObjectOutputStream;
  19. import java.io.ObjectInputStream;
  20. import java.io.IOException;
  21.  
  22.  
  23. /**
  24.  * This is a utility class that can be used by beans that support bound
  25.  * properties.  You can use an instance of this class as a member field
  26.  * of your bean and delegate various work to it.
  27.  */
  28.  
  29. public class PropertyChangeSupport implements java.io.Serializable {
  30.  
  31.     /**
  32.      * @sourceBean  The bean to be given as the source for any events.
  33.      */
  34.  
  35.     public PropertyChangeSupport(Object sourceBean) {
  36.     source = sourceBean;
  37.     }
  38.  
  39.     /**
  40.      * Add a PropertyChangeListener to the listener list.
  41.      *
  42.      * @param listener  The PropertyChangeListener to be added
  43.      */
  44.  
  45.     public synchronized void addPropertyChangeListener(
  46.                 PropertyChangeListener listener) {
  47.     if (listeners == null) {
  48.         listeners = new java.util.Vector();
  49.     }
  50.     listeners.addElement(listener);
  51.     }
  52.  
  53.     /**
  54.      * Remove a PropertyChangeListener from the listener list.
  55.      *
  56.      * @param listener  The PropertyChangeListener to be removed
  57.      */
  58.  
  59.     public synchronized void removePropertyChangeListener(
  60.                 PropertyChangeListener listener) {
  61.     if (listeners == null) {
  62.         return;
  63.     }
  64.     listeners.removeElement(listener);
  65.     }
  66.  
  67.     /**
  68.      * Report a bound property update to any registered listeners.
  69.      * No event is fired if old and new are equal and non-null.
  70.      *
  71.      * @param propertyName  The programmatic name of the property
  72.      *        that was changed.
  73.      * @param oldValue  The old value of the property.
  74.      * @param newValue  The new value of the property.
  75.      */
  76.     public void firePropertyChange(String propertyName, 
  77.                     Object oldValue, Object newValue) {
  78.  
  79.     if (oldValue != null && oldValue.equals(newValue)) {
  80.         return;
  81.     }
  82.  
  83.     java.util.Vector targets;
  84.     synchronized (this) {
  85.         if (listeners == null) {
  86.             return;
  87.         }
  88.         targets = (java.util.Vector) listeners.clone();
  89.     }
  90.         PropertyChangeEvent evt = new PropertyChangeEvent(source,
  91.                         propertyName, oldValue, newValue);
  92.  
  93.     for (int i = 0; i < targets.size(); i++) {
  94.         PropertyChangeListener target = (PropertyChangeListener)targets.elementAt(i);
  95.         target.propertyChange(evt);
  96.     }
  97.     }
  98.  
  99.  
  100.     private void writeObject(ObjectOutputStream s) throws IOException {
  101.         s.defaultWriteObject();
  102.  
  103.     java.util.Vector v = null;
  104.     synchronized (this) {
  105.         if (listeners != null) {
  106.             v = (java.util.Vector) listeners.clone();
  107.             }
  108.     }
  109.  
  110.     if (v != null) {
  111.         for(int i = 0; i < v.size(); i++) {
  112.             PropertyChangeListener l = (PropertyChangeListener)v.elementAt(i);
  113.             if (l instanceof Serializable) {
  114.                 s.writeObject(l);
  115.             }
  116.             }
  117.         }
  118.         s.writeObject(null);
  119.     }
  120.  
  121.  
  122.     private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
  123.         s.defaultReadObject();
  124.       
  125.         Object listenerOrNull;
  126.         while(null != (listenerOrNull = s.readObject())) {
  127.       addPropertyChangeListener((PropertyChangeListener)listenerOrNull);
  128.         }
  129.     }
  130.  
  131.     transient private java.util.Vector listeners;
  132.     private Object source;
  133.     private int propertyChangeSupportSerializedDataVersion = 1;
  134. }
  135.  
  136.  
  137.  
  138.  
  139.